package com.activities;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.Animations.AnimUtil;
import com.Data.User;
import com.Data.User.onAddUserCompleted;
import com.Managers.FragmentManagerClass;
import com.Managers.ThemeManager;
import com.Managers.UserManagerClass;
import com.TextFormat.UtilFonts;
import com.shareup.R;
import com.utils.ShowToastClass;


public class ActivityRegister extends Activity {

    private ImageView backButton;

    private final OnClickListener BackClick = new OnClickListener() {

        @Override
        public void onClick(View v) {

            exit();
        }
    };

    private ImageView rButton;

    private final OnClickListener RegClick = new OnClickListener() {

        @Override
        public void onClick(View v) {

            validar();
        }
    };

    private EditText regEmail;

    private RadioButton regFemale;

    private RadioButton regMale;

    private EditText regPassword;

    private EditText regRealName;

    private EditText regUserName;

    private TextView title_info;

    private TextView title_login_info;

    private TextView title_user_info;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            //
            return true;
        }
        //
        return super.onKeyUp(keyCode, event);
    }

    private void addedUser(String result) {

        String[] res = result.split("#");
        //
        if (res.length > 0) {
            if (res[0].equals("-10")) {

                ShowToastClass.message(getResources().getString(R.string.error_connection_timeout), this);
            }
            else
                if (res[0].equals("-20")) {
                    ShowToastClass.message(getResources().getString(R.string.error_user_exists), this);
                }
                else {
                    //
                    User newUser = new User();
                    newUser.setID(Integer.parseInt(res[0]));
                    newUser.setProfilePicture(res[1]);
                    // SAVIN' INFO
                    UserManagerClass usm = new UserManagerClass(this);
                    usm.saveInfo(newUser);
                    // FINISHING LOGIN ACTIVITY
                    FragmentManagerClass.aLogin.finish();
                    //
                    Intent intent = new Intent(this, ActivityMain.class);
                    startActivity(intent);
                    AnimUtil.getActivityTransition(this, true);

                    finish();

                }
        }
        else {

            ShowToastClass.message(res[0], this);
        }
    }

    private void continuar() {

        User us = new User();
        us.setRealName(regRealName.getText().toString());
        us.setEmail(regEmail.getText().toString());
        us.setUsername(regUserName.getText().toString());
        us.setPassword(regPassword.getText().toString());
        int sex = 1;
        //
        if (regMale.isChecked()) {
            sex = 0;
        }
        us.setSex(sex);
        //
        us.setContext(this);
        us.setCallbackAddUser(new onAddUserCompleted() {

            @Override
            public void Completed(String result) {

                addedUser(result);

            }
        });
        us.execute(User.ACTION_ADD);

    }

    private void exit() {

        finish();
        AnimUtil.getActivityTransition(this, false);
    }

    private void inicializar() {

        rButton = (ImageView) this.findViewById(R.id.login_done_button);
        rButton.setOnClickListener(RegClick);
        //
        backButton = (ImageView) this.findViewById(R.id.reg_image_close);
        backButton.setOnClickListener(BackClick);
        //
        regUserName = (EditText) this.findViewById(R.id.reg_username);
        regPassword = (EditText) this.findViewById(R.id.reg_password);
        regRealName = (EditText) this.findViewById(R.id.reg_realname);
        regMale = (RadioButton) this.findViewById(R.id.reg_male);
        regFemale = (RadioButton) this.findViewById(R.id.reg_female);
        regEmail = (EditText) this.findViewById(R.id.reg_email);
        regEmail.setOnEditorActionListener(new EditText.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    validar();
                    return true;
                }
                return false;
            }
        });
        //
        title_login_info = (TextView) this.findViewById(R.id.reg_title_login_info);
        title_user_info = (TextView) this.findViewById(R.id.reg_title_user_info);
        title_info = (TextView) this.findViewById(R.id.title_actionbar_main);
        //
        UtilFonts.setTextBoldFont(title_info);
        UtilFonts.setTextBoldFont(title_login_info);
        UtilFonts.setTextBoldFont(title_user_info);
        UtilFonts.setTextRegularFont(regUserName);
        UtilFonts.setTextRegularFont(regPassword);
        UtilFonts.setTextRegularFont(regRealName);
        UtilFonts.setTextRegularFont(regEmail);
        UtilFonts.setTextRegularFont(regMale);
        UtilFonts.setTextRegularFont(regFemale);
        //
    }

    private void validar() {

        if (regUserName.getText().toString().isEmpty()
                    || regPassword.getText().toString().isEmpty()
                    || regRealName.getText().toString().isEmpty()
                    || regEmail.getText().toString().isEmpty()) {
            ShowToastClass.message(this.getResources().getString(R.string.error_empty_fields), this);
        }
        else {
            if (regUserName.getText().length() < 3) {
                regUserName.setError(this.getResources().getString(R.string.error_field_username_short));
            }
            else
                if (regPassword.getText().length() < 6) {
                    regPassword.setError(this.getResources().getString(R.string.error_field_pass_short));
                }
                else
                    if (regRealName.getText().length() < 4) {
                        regRealName.setError(this.getResources().getString(R.string.error_field_name_short));
                    }
                    else {
                        if (!regEmail.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")) {
                            regEmail.setError(this.getResources().getString(R.string.error_email_match));
                        }
                        else {
                            continuar();
                        }
                    }
        }
    }

    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ThemeManager.setTheme(this);
        setContentView(R.layout.activity_register);
        super.onCreate(savedInstanceState);

        inicializar();
    }
}
